/*
  学习目标：动态路由
 
*/
import React from 'react';
import { BrowserRouter as Router, Route, NavLink, Switch, Redirect, Link } from 'react-router-dom';
import './index.css';

export default class App extends React.Component {
  render() {
    return (
      <Router>
        <div className="App">
          <h1>app组件</h1>
          <NavLink to="/home" exact>
            调到home
          </NavLink>
          <br />
          <NavLink to="/my">调到我的音乐</NavLink> <br />
          <NavLink to="/friend">调到朋友</NavLink>
          <hr />
          <Switch>
            <Redirect from="/" to="/home" exact />
            <Route path="/home" component={Home}></Route>
            <Route path="/my" component={MyMusic}></Route>
            <Route path="/friend" component={Friend}></Route>
            {/* 1. 路径改造为动态路由 */}
            <Route path="/detail/:xxx" component={Detail} />
            <Route component={NotFound}></Route>
          </Switch>
        </div>
      </Router>
    );
  }
}

// 0. 新建组件
const Detail = (props) => {
  // 2. 获取动态路由参数： props.match.params.xxx
  return <h1>详情 -{props.match.params.xxx}</h1>;
};

function Home({ history }) {
  return (
    <>
      <h1>我是首页组件</h1>
      <button
        onClick={() => {
          history.go(-1);
        }}
      >
        调到朋友
      </button>
    </>
  );
}

function Friend() {
  return (
    <div>
      我是朋友组件
      <Link to="/friend/f1">朋友孩子1</Link> <br />
      <Link to="/friend/f2">朋友孩子2</Link>
      <br />
      <Link to="/friend/f3">朋友孩子3</Link> <br />
    </div>
  );
}

const NotFound = () => <h1>404页面</h1>;

function MyMusic() {
  return <h1>我是我的音乐件</h1>;
}
